/************************************************************
  GSM Modem SIM900 driver (external interface)
  Ref.
    http://www.mt-system.ru/documents/sim900_atc_v1.03.pdf
    http://www.mt-system.ru/documents/an_sim900_ip_application%20note_v1.00_draft.pdf
************************************************************/

#ifndef GSM_H_
#define GSM_H_

#include <stdio.h>
#include <string.h>
#include <stdbool.h>

#include <ctl.h>

#include "../led.h"
#include "../modem.h"
#include "../../fatfs/ff.h"
#include "../../utils/utils.h"
#include "../../config/config.h"
#include "../../utils/json/json.h"


//#define SIM300

//TODO: remove this
#define GSM_GPRS_APN "internet"

typedef enum
{
  GSM_SIM1 = 0,
  GSM_SIM2
} GSM_SIM_t;

typedef enum {
  GSM_INACTIVE = 0,
  GSM_DISCONNECTED,
  GSM_CONNECTED, 
  GSM_ERROR,
  GSM_FORMAT_COMMAND
} GSM_STATUS_t;

typedef enum{// результат выполнения HTTP-запроса
  HTTP_ERROR=0,
  HTTP_NO_UPDATE,
  HTTP_FILE_LOAD,
  HTTP_FORMAT_REQUEST
}HTTP_GET_RESULT;


/* Return modem status */
GSM_STATUS_t gsm_status();

/* Power on GSM modem and initialize GSM network. */
GSM_STATUS_t gsm_enable(char* apn, GSM_SIM_t sim, int baud);

/* Connect modem to a specified host and port */
GSM_STATUS_t gsm_connect(char* host, unsigned int port, unsigned int TcpTimeout);

/* Disconnect modem from a host */
GSM_STATUS_t gsm_disconnect();

/* Power GSM off */
GSM_STATUS_t gsm_disable();

/* Transmit RTEA encrypted data to a server */
GSM_STATUS_t gsm_transmit_json(char* request, unsigned int length, json_node** response, bool encrypt);

/* Transmit raw data to a server */
GSM_STATUS_t gsm_http_download(char* host, char* path, char* filename, unsigned int* crc32, unsigned long timeout_ms);

#endif /* GSM_H_ */
